import { getNetworkAmountLimit } from '@/api/network'
import { ref } from 'vue'

/**
 * 网点销售金额服务
 */
export const useNetworkAmountLimitService = () => {
  const getAmountLimitLoading = ref(false)
  const networkAmountLimit = ref<NetworkAPIResponse.NetworkAmountLimit>({
    id: '',
    networkId: '',
    freeAmountPercentage: 0,
    freezeAmount: 0,
    grantAmount: 0,
    broughtForwardAmount: 0,
    saleAmount: 0,
    freeAmount: 0,
    annualTotalAmount: 0,
  })

  const getNetworkAmount = async (networkId: string) => {
    try {
      getAmountLimitLoading.value = true
      const res = await getNetworkAmountLimit(networkId)
      getAmountLimitLoading.value = false
      if (res.data.code === 20000) {
        networkAmountLimit.value = res.data.data.row
        return ''
      }
      return res.data.message
    } catch (error) {
      getAmountLimitLoading.value = false
      return '获取网点销售金额失败，请稍候重试'
    }
  }

  return {
    networkAmountLimit,
    getAmountLimitLoading,
    getNetworkAmount,
  }
}
